//
//  ShopTableViewCell.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/23.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopTableViewCell.h"

@interface ShopTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * telLabel;
@property (nonatomic, weak)UILabel * orderNumLabel;
@property (nonatomic, weak)UILabel * stateLabel;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIImageView * shopImageView;
@property (nonatomic, weak)UILabel * goodsNameLabel;
@property (nonatomic, weak)UILabel * shopNameLabel;
@property (nonatomic, weak)UILabel * dateLabel;
@property (nonatomic, weak)UILabel * moneyLabel;
@property (nonatomic, weak)UIView * smallView;
@property (nonatomic, weak)UILabel * originalLabel;
@property (nonatomic, weak)UILabel * soldLabel;
//@property (nonatomic, weak)UIView * greenView;
//@property (nonatomic, weak)UILabel * completeTimeLabel;
//@property (nonatomic, weak)UIImageView * checkImageView;
@property (nonatomic, weak)UIImageView * tuanImageView;

@end


@implementation ShopTableViewCell


- (void)setModel:(ShopOrderModel *)model{
    
    _model = model;
    
    if([model.head_img containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:model.head_img];
        [self.iconImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.head_img];
        NSURL * iconUrl = [NSURL URLWithString:iconStr];
        [self.iconImageView sd_setImageWithURL:iconUrl];
    }
    
    NSString * goodsStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.merchants_goods_img];
    NSURL * goodsUrl = [NSURL URLWithString:goodsStr];
    [self.shopImageView sd_setImageWithURL:goodsUrl];
    self.nameLabel.text = model.user_name;
    self.timeLabel.text = model.goods_order_inputtime;
    self.telLabel.text = model.phone;
    self.orderNumLabel.text = [NSString stringWithFormat:@"订单号:%@",model.goods_order_no];
    self.goodsNameLabel.text = model.merchants_goods_name;
    self.shopNameLabel.text = model.merchants_name;
    self.moneyLabel.text = [NSString stringWithFormat:@"￥ %@",model.merchants_goods_discount_price];
//    self.originalLabel.text = [NSString stringWithFormat:@"原价 ￥%@",model.merchants_goods_price];
    NSString * orString = [NSString stringWithFormat:@"￥%@",model.merchants_goods_price];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:orString attributes:attribtDic];
    
    self.originalLabel.attributedText = attribtStr;
    
    self.soldLabel.text = [NSString stringWithFormat:@"已售:%@",model.merchants_goods_sold];
//    self.dateLabel.text = [NSString stringWithFormat:@"%@",model.merchants_goods_available_time];
    int status = [model.goods_order_status intValue];
    if (status == 0){
        
        self.stateLabel.text = @"待付款";
        self.stateLabel.textColor = ColorWithRGB(0x52d498);
    }else if (status == 1){
        
        self.stateLabel.text = @"待使用";
        self.stateLabel.textColor = ColorWithRGB(0x52d498);
    }else if (status == 3){

        self.stateLabel.text = @"待评价";
        self.stateLabel.textColor = ColorWithRGB(0x52d498);
    }else if (status == 2){
        
        self.stateLabel.text = @"已取消";
        self.stateLabel.textColor = [UIColor lightGrayColor];
    }else if (status == 4){
        
        self.stateLabel.text = @"已失效";
        self.stateLabel.textColor = [UIColor lightGrayColor];
    }else if (status == 5){
        
        self.stateLabel.text = @"已评价";
        self.stateLabel.textColor = [UIColor lightGrayColor];
    }
    
    int merchants_goods_type = [model.merchants_goods_type intValue];
    if (merchants_goods_type == 1) {
        
        self.tuanImageView.hidden = YES;
        self.dateLabel.text = [NSString stringWithFormat:@"%@",model.merchants_goods_available_time];
    }else{
        
        self.tuanImageView.hidden = NO;
//        NSDictionary * dict = [self dictionaryWithJsonString:model.merchants_goods_available_time];
//
//        NSArray *keysArray = [dict allKeys];
//        NSMutableArray * keyMuArr = [NSMutableArray array];
//        for (NSString * week in keysArray) {
//
//            int w = [week intValue];
//            [keyMuArr addObject:@(w)];
//        }
//        NSArray * weekArr = [self arraySortBreakWith:keyMuArr.copy];
//        //        NSLog(@"keyMuArr===%@",keyMuArr);
//        NSMutableArray * resMuArr = [NSMutableArray array];
//        for (NSString * week in weekArr) {
//
//            int w = [week intValue];
//            NSString * d = [self isEqualWeekWith:w];
//
//            [resMuArr addObject:d];
//        }
//
//        NSString * res = [resMuArr.copy componentsJoinedByString:@","];
//
//        NSString * key = keysArray.firstObject;
//        NSArray * value = dict[key];
//        NSString *string = [value componentsJoinedByString:@","];
        
        self.dateLabel.text = [NSString stringWithFormat:@"场次:%@",model.goods_order_goods_field];
    }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF8F9FA);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.backgroundColor = [UIColor lightGrayColor];
    iconImageView.layer.cornerRadius = 12.5 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.text = @"用户名";
    nameLabel.textColor = ZRBlackColor;
    nameLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
    
    self.nameLabel = nameLabel;
    
    [self.backView addSubview:self.nameLabel];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.text = @"下单";
    exploreLabel.textColor = ZRBlackColor;
    exploreLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.exploreLabel = exploreLabel;
    
    [self.backView addSubview:self.exploreLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.text = @"刚刚";
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UILabel * telLabel = [UILabel new];
    
    telLabel.text = @"186819999921";
    telLabel.textColor = [UIColor lightGrayColor];
    telLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.telLabel = telLabel;
    
    [self.backView addSubview:self.telLabel];
    //
    
    UILabel * orderNumLabel = [UILabel new];
    
    orderNumLabel.text = @"订单号:129-39-21";
    orderNumLabel.textColor = [UIColor lightGrayColor];
    orderNumLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.orderNumLabel = orderNumLabel;
    
    [self.backView addSubview:self.orderNumLabel];
    
    
//    UIButton * verifyButton = [UIButton new];
//
//    [verifyButton setTitle:@"验证" forState:UIControlStateNormal];
//    [verifyButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
//    [verifyButton setBackgroundColor:ColorWithRGB(0xFFE055)];
//    verifyButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
//    verifyButton.layer.cornerRadius = 5 * SCREENH6;
//    verifyButton.layer.masksToBounds = YES;
//
//    self.verifyButton = verifyButton;
//
//    [self.backView addSubview:self.verifyButton];
    
    UILabel * stateLabel = [UILabel new];
    
    stateLabel.textColor = ColorWithRGB(0x52d498);
    stateLabel.text = @"待使用";
    stateLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    stateLabel.textAlignment = NSTextAlignmentRight;
    
    self.stateLabel = stateLabel;
    
    [self.backView addSubview:self.stateLabel];

    
//    UIView * greenView = [UIView new];
//    
//    greenView.backgroundColor = ColorWithRGB(0x53d499);
//    greenView.layer.cornerRadius = 5;
//    greenView.layer.masksToBounds = YES;
//    greenView.hidden = YES;
//    
//    self.greenView = greenView;
//    
//    [self.backView addSubview:self.greenView];
//    
//    
//    UIImageView * checkImageView = [UIImageView new];
//    
//    checkImageView.image = [UIImage imageNamed:@"checked-symbolcopy"];
//    
//    self.checkImageView = checkImageView;
//    
//    [self.greenView addSubview:self.checkImageView];
//    
//    
//    UILabel * completeLabel = [UILabel new];
//    
//    completeLabel.textColor = ZRWhiteColor;
//    completeLabel.font = [UIFont systemFontOfSize:9 * SCREENH6];
//    completeLabel.numberOfLines = 2;
//    
//    self.completeTimeLabel = completeLabel;
//    
//    [self.greenView addSubview:self.completeTimeLabel];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF8F9FA);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
    UIImageView * shopImageView = [UIImageView new];
    
    shopImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.shopImageView = shopImageView;
    
    [self.backView addSubview:self.shopImageView];
    
    UILabel * goodsLabel = [UILabel new];
    
    goodsLabel.textColor = ZRBlackColor;
    goodsLabel.text = @"商品名";
    goodsLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.goodsNameLabel = goodsLabel;
    
    [self.backView addSubview:self.goodsNameLabel];
 
    
    UILabel * shopNameLabel = [UILabel new];
    
    shopNameLabel.textColor = ZRBlackColor;
    shopNameLabel.text = @"店铺名";
    shopNameLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.shopNameLabel = shopNameLabel;
    
    [self.backView addSubview:self.shopNameLabel];
    
    
    UILabel * dateLabel = [UILabel new];
    
    dateLabel.textColor = ZRBlackColor;
    dateLabel.text = @"9月21日 - 11月3日";
    dateLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.dateLabel = dateLabel;
    
    [self.backView addSubview:self.dateLabel];
    
    UIView * smallView = [UIView new];
    
    smallView.backgroundColor = ColorWithRGB(0xFFE055);
    
    self.smallView = smallView;

    [self.backView addSubview:self.smallView];
    
    UILabel * moneyLabel = [UILabel new];
    
    moneyLabel.textColor = ZRBlackColor;
    moneyLabel.text = @"$ 20.9";
    moneyLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
    
    self.moneyLabel = moneyLabel;
    
    [self.backView addSubview:self.moneyLabel];
    
    
    UILabel * originalLabel = [UILabel new];
    
    originalLabel.textColor = ZRBlackColor;
    originalLabel.text = @"原价 $30.2";
    originalLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.originalLabel = originalLabel;
    
    [self.backView addSubview:self.originalLabel];
    
    
    UILabel * soldLabel = [UILabel new];
    
    soldLabel.textColor = [UIColor lightGrayColor];
    soldLabel.text = @"已售 24";
    soldLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.soldLabel = soldLabel;
    
    [self.backView addSubview:self.soldLabel];
    
    
    UIImageView * tuanImageView = [UIImageView new];
    
    tuanImageView.image = [UIImage imageNamed:@"首页拼团"];
    tuanImageView.hidden = YES;
    
    self.tuanImageView = tuanImageView;
    
    [self.contentView addSubview:self.tuanImageView];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.height.mas_equalTo(155 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(8 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.exploreLabel);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
    }];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(2 * SCREENH6);
    }];
    
    [self.orderNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.telLabel);
        make.top.mas_equalTo(self.telLabel.mas_bottom).offset(2 * SCREENH6);
    }];
    
//    [self.verifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.mas_equalTo(self.timeLabel);
//        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5 * SCREENH6);
//        make.height.mas_equalTo(25 * SCREENH6);
//        make.width.mas_equalTo(45 * SCREENH6);
//    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.timeLabel);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5 * SCREENH6);

    }];
    
//    [self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.mas_equalTo(self.timeLabel);
//        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(2 * SCREENH6);
//        make.height.mas_equalTo(35 * SCREENH6);
//        make.width.mas_equalTo(80 * SCREENH6);
//    }];
//
//    [self.checkImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.greenView);
//        make.top.mas_equalTo(self.greenView).offset(2 * SCREENH6);
//        make.width.mas_equalTo(11 * SCREENH6);
//        make.height.mas_equalTo(9 * SCREENH6);
//    }];
//
//    [self.completeTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.greenView).offset(15 * SCREENH6);
//        make.trailing.mas_equalTo(self.greenView).offset(-5 * SCREENH6);
//        make.top.mas_equalTo(self.checkImageView.mas_bottom).offset(3 * SCREENH6);
//    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.stateLabel);
        make.leading.mas_equalTo(self.orderNumLabel);
        make.top.mas_equalTo(self.orderNumLabel.mas_bottom).offset(5 * SCREENH6);
        make.height.mas_equalTo(2 * SCREENH6);
    }];
    
    [self.shopImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(5 * SCREENH6);
        make.width.mas_equalTo(58 * SCREENH6);
        make.height.mas_equalTo(60 * SCREENH6);
    }];
    
    [self.tuanImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView);
        make.centerY.mas_equalTo(self.shopImageView);
        make.width.mas_equalTo(45 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopImageView.mas_trailing).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.stateLabel.mas_leading).offset(-5 * SCREENH6);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.shopNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsNameLabel);
        make.trailing.mas_equalTo(self.tuanImageView.mas_leading).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.goodsNameLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopNameLabel);
        make.trailing.mas_equalTo(self.tuanImageView.mas_leading).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.shopNameLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.shopImageView.mas_trailing).offset(20 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(8 * SCREENH6);
        make.height.mas_equalTo(10 * SCREENH6);
        make.width.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(2 * SCREENH6);
    }];
    
    [self.originalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.moneyLabel.mas_trailing).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(4 * SCREENH6);
    }];
    
    [self.soldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(7 * SCREENH6);
    }];
}

- (NSString *)isEqualWeekWith:(int)week{
    
    NSString * weekDay;
    switch (week) {
        case 1:
            weekDay = @"周一";
            break;
        case 2:
            weekDay = @"周二";
            break;
        case 3:
            weekDay = @"周三";
            break;
        case 4:
            weekDay = @"周四";
            break;
        case 5:
            weekDay = @"周五";
            break;
        case 6:
            weekDay = @"周六";
            break;
        case 7:
            weekDay = @"周日";
            break;
        default:
            break;
    }
    
    return weekDay;
}


#pragma mark -- 数组排序方法（乱序）
- (NSArray *)arraySortBreakWith:(NSArray *)array{
    
    //对数组进行排序
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj1 compare:obj2]; //升序
        
    }];
    
    return result;
    
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
